{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 导入库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.chrome.service import Service\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "import time\n",
    "import os\n",
    "from selenium.webdriver.common.by import By"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.定位一组元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"\n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "\n",
    "# 打开本地 HTML 文件\n",
    "url = \"file:///\" + os.path.abspath(r\"E:\\test notebook\\seleium\\temp\\html\\checkbox.html\")\n",
    "driver.get(url)\n",
    "time.sleep(3)\n",
    "driver.maximize_window()\n",
    "\n",
    "# 勾选里面所有的checkbox\n",
    "# driver.find_element(By.ID, \"c1\").click()\n",
    "# driver.find_element(By.ID, \"c2\").click()\n",
    "# driver.find_element(By.ID, \"c3\").click()\n",
    "# 定位一组tag name 都为input的元素\n",
    "\n",
    "# 勾选所有 checkbox\n",
    "buttons = driver.find_elements(By.TAG_NAME, \"input\")\n",
    "for button in buttons:\n",
    "    if button.get_attribute('type') == 'radio':\n",
    "        button.click()\n",
    "\n",
    "time.sleep(3)\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.多层框架定位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"\n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "\n",
    "# 打开本地 HTML 文件\n",
    "url = \"file:///\" + os.path.abspath(r\"E:\\test notebook\\seleium\\temp\\html\\frame.html\")\n",
    "driver.get(url)\n",
    "time.sleep(1)\n",
    "driver.maximize_window()\n",
    "time.sleep(3)\n",
    "\n",
    "# 进入名为 f1 的 iframe 框架\n",
    "driver.switch_to.frame(\"f1\")\n",
    "driver.find_element(By.LINK_TEXT, \"click\").click()\n",
    "\n",
    "\n",
    "time.sleep(6)\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.层级定位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium.webdriver.common.action_chains import ActionChains\n",
    "# 指定 ChromeDriver 和 Chrome 浏览器路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"\n",
    "\n",
    "# 配置 Chrome 启动选项\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path\n",
    "\n",
    "# 创建服务对象并启动浏览器\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "\n",
    "# 打开本地 HTML 文件\n",
    "url = \"file:///\" + os.path.abspath(r\"E:\\test notebook\\seleium\\temp\\html\\level_locate.html\")\n",
    "driver.get(url)\n",
    "\n",
    "driver.maximize_window()\n",
    "time.sleep(3)\n",
    "# 点击 Link 1\n",
    "driver.find_element(By.LINK_TEXT, \"Link1\").click()\n",
    "\n",
    "time.sleep(3)\n",
    "\n",
    "# 鼠标移动到 Another action 元素上\n",
    "action = driver.find_element(By.LINK_TEXT, \"Another action\")\n",
    "ActionChains(driver).move_to_element(action).perform()\n",
    "\n",
    "time.sleep(3)\n",
    "driver.quit()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.下拉框选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 指定 ChromeDriver 和 Chrome 浏览器路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"\n",
    "\n",
    "# 配置 Chrome 启动选项\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path\n",
    "\n",
    "# 创建服务对象并启动浏览器\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "url = \"file:///\"+os.path.abspath(r\"E:\\test notebook\\seleium\\temp\\html\\drop_down.html\")\n",
    "driver.get(url)\n",
    "driver.maximize_window()\n",
    "time.sleep(3)\n",
    "# xpath\n",
    "#driver.find_element(By.XPATH,\"//*[@id='ShippingMethod']/option[6]\").click()\n",
    "options = driver.find_elements(By.TAG_NAME, \"option\")\n",
    "# for option in options:\n",
    "#     if option.get_attribute('value') == '9.25':\n",
    "#         option.click()\n",
    "options[5].click()\n",
    "time.sleep(3)\n",
    "driver.quit()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.alert弹框处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 指定 ChromeDriver 和 Chrome 浏览器路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"\n",
    "\n",
    "# 配置 Chrome 启动选项\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path\n",
    "\n",
    "# 创建服务对象并启动浏览器\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "url = \"file:///\"+os.path.abspath(r\"temp\\html\\alert.html\")\n",
    "driver.get(url)\n",
    "driver.maximize_window()\n",
    "time.sleep(1)\n",
    "\n",
    "# 定位元素，点击，使得弹出框出现\n",
    "driver.find_element(By.ID,\"tooltip\").click()\n",
    "time.sleep(2)\n",
    "# 定位弹出框/获得弹出框的操作句柄\n",
    "alert = driver.switch_to.alert\n",
    "time.sleep(2)\n",
    "# 关闭alert\n",
    "alert.accept()\n",
    "time.sleep(2)\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 指定 ChromeDriver 和 Chrome 浏览器路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"\n",
    "\n",
    "# 配置 Chrome 启动选项\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path\n",
    "\n",
    "# 创建服务对象并启动浏览器\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "\n",
    "url = \"file:///\"+os.path.abspath(r\"temp\\html\\send.html\")\n",
    "driver.get(url)\n",
    "driver.maximize_window()\n",
    "time.sleep(3)\n",
    "\n",
    "driver.find_element(By.TAG_NAME, \"input\").click()\n",
    "# 先获得弹出框的操作句柄\n",
    "alert = driver.switch_to.alert\n",
    "alert.send_keys(\"严瑞杰\")\n",
    "\n",
    "time.sleep(4)\n",
    "# 关闭信息展示弹框\n",
    "alert.accept()\n",
    "\n",
    "time.sleep(3)\n",
    "\n",
    "driver.quit()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.div块处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "div按钮 1 的文本内容：×\n",
      "div按钮 2 的文本内容：Close\n",
      "div按钮 3 的文本内容：Save changes\n"
     ]
    }
   ],
   "source": [
    "# 指定 ChromeDriver 和 Chrome 浏览器路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"\n",
    "\n",
    "# 配置 Chrome 启动选项\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path\n",
    "\n",
    "# 创建服务对象并启动浏览器\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "url = \"file:///\"+os.path.abspath(r\"temp\\html\\modal.html\")\n",
    "driver.get(url)\n",
    "driver.maximize_window()\n",
    "time.sleep(1)\n",
    "# 点击click 出现弹框\n",
    "driver.find_element(By.LINK_TEXT, \"Click\").click()\n",
    "time.sleep(2)\n",
    "\n",
    "# 点击div 框框里面的click me，让弹出框内容发生变化\n",
    "div1 = driver.find_element(By.CLASS_NAME, \"modal-body\")\n",
    "div1.find_element(By.LINK_TEXT, \"click me\").click()\n",
    "time.sleep(2)\n",
    "\n",
    "# 定位div 在定位具体的button\n",
    "div2 = driver.find_element(By.CLASS_NAME, \"modal-footer\")\n",
    "buttons = driver.find_elements(By.TAG_NAME, \"button\")\n",
    "for i, button in enumerate(buttons):\n",
    "    print(f\"div按钮 {i+1} 的文本内容：{button.text}\")\n",
    "buttons[1].click()\n",
    "\n",
    "time.sleep(3)\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "div2按钮 1 的文本内容：Close\n",
      "div2按钮 2 的文本内容：Save changes\n"
     ]
    }
   ],
   "source": [
    "# 指定 ChromeDriver 和 Chrome 浏览器路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"\n",
    "\n",
    "# 配置 Chrome 启动选项\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path\n",
    "\n",
    "# 创建服务对象并启动浏览器\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "url = \"file:///\"+os.path.abspath(r\"temp\\html\\modal.html\")\n",
    "driver.get(url)\n",
    "driver.maximize_window()\n",
    "time.sleep(1)\n",
    "# 点击click 出现弹框\n",
    "driver.find_element(By.LINK_TEXT, \"Click\").click()\n",
    "time.sleep(2)\n",
    "\n",
    "# 点击div 框框里面的click me，让弹出框内容发生变化\n",
    "div1 = driver.find_element(By.CLASS_NAME, \"modal-body\")\n",
    "div1.find_element(By.LINK_TEXT, \"click me\").click()\n",
    "time.sleep(2)\n",
    "\n",
    "# 定位div 在定位具体的button\n",
    "div2 = driver.find_element(By.CLASS_NAME, \"modal-footer\")\n",
    "\n",
    "button2s = div2.find_elements(By.TAG_NAME,\"button\")\n",
    "for i, button in enumerate(button2s):\n",
    "    print(f\"div2按钮 {i+1} 的文本内容：{button.text}\")\n",
    "button2s[0].click()\n",
    "time.sleep(3)\n",
    "\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 7.上传文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 指定 ChromeDriver 和 Chrome 浏览器路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"\n",
    "\n",
    "# 配置 Chrome 启动选项\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path\n",
    "\n",
    "# 创建服务对象并启动浏览器\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "\n",
    "url = \"file:///\"+os.path.abspath(r\"temp\\html\\upload.html\")\n",
    "driver.get(url)\n",
    "driver.maximize_window()\n",
    "time.sleep(3)\n",
    "\n",
    "driver.find_element(By.NAME, \"file\").send_keys(r\"C:\\Users\\12525\\OneDrive\\桌面\\123\\屏幕截图 2025-02-19 200952.png\")\n",
    "\n",
    "time.sleep(3)\n",
    "driver.quit()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
